#include "application.h"
#include <QScreen>
#include <QTimer>

Application::Application(int &argc, char **argv)
    : QApplication(argc, argv)
    , m_mainWindow(nullptr)
    , m_splash(nullptr)
{
}

Application::~Application()
{
}

void Application::start()
{
    // 顯示 Splash
    showSplash();
    
    // 創建主窗口但先不顯示
    m_mainWindow = new MainWindow();
    
    // 在主窗口準備好后再顯示它
    QTimer::singleShot(0, this, [this]() {
        m_mainWindow->show();
        if (m_splash) {
            m_splash->finish(m_mainWindow);
            m_splash->deleteLater();
            m_splash = nullptr;
        }
    });
}

void Application::showSplash()
{
    // 加載啟動圖片
    QPixmap splashPixmap(":/images/splash.png");
    
    // 獲取屏幕尺寸
    QScreen *screen = primaryScreen();
    QSize screenSize = screen->availableSize();
    
    // 計算合適的 splash 尺寸（例如屏幕寬度的 40%）
    int targetWidth = screenSize.width() * 0.4;
    QSize targetSize(targetWidth, targetWidth * 9 / 16);  // 保持 16:9 的比例
    
    // 縮放圖片
    QPixmap scaledPixmap = splashPixmap.scaled(targetSize, 
                                              Qt::KeepAspectRatio, 
                                              Qt::SmoothTransformation);
    
    // 創建並顯示 splash
    m_splash = new SplashScreen(scaledPixmap);
    m_splash->show();
}